package com.chen.springboot.controller;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.springboot.common.Result;
import com.chen.springboot.entity.Order;
import com.chen.springboot.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Api(tags = {"订单接口"})
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("新建订单")
    @PostMapping
    public Result<?> save(@RequestBody Order order){
        orderService.save(order);
        return Result.success();
    }

    @ApiOperation("更新订单")
    @PutMapping
    public Result<?> update(@RequestBody Order order){
        orderService.update(order);
        return Result.success();
    }

    @ApiOperation("发货")
    @PutMapping("/ship/{orderNo}")
    public Result<?> ship(@PathVariable String orderNo){
        orderService.ship(orderNo);
        return Result.success();
    }

    @ApiOperation("根据订单ID删除订单")
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        orderService.delete(id);
        return Result.success();
    }

    @ApiOperation("删除所选订单")
    @DeleteMapping("/select")
    public Result<?> deleteAll(@RequestBody List<Long> ids){
        orderService.deleteAll(ids);
        return Result.success();
    }

    @ApiOperation("根据订单ID查询订单")
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id){
        Order data = orderService.getById(id);
        return Result.success(data);
    }

    @ApiOperation("单独购买一个商品")
    @GetMapping("/buy")
    public Result<?> buy(@RequestParam Long itemId,@RequestParam Integer userId,@RequestParam Integer count){
        orderService.buy(itemId,userId,count);
        //跳转页面
        String url="http://localhost:9876/myOrder";
        return Result.success(url);
    }

    @ApiOperation("购物车结算")
    @PostMapping("/buyCart")
    public Result<?> buyCart(@RequestBody List<Order> orders){
        orderService.buyCart(orders);
        //跳转页面
        String url="http://localhost:9876/myOrder";
        return Result.success(url);
    }

    @ApiOperation("添加商品到购物车")
    @GetMapping("/add")
    public Result<?> addToCart(@RequestParam Long itemId,@RequestParam Integer userId,@RequestParam Integer count){
        orderService.addToCart(itemId,userId,count);
        return Result.success();
    }

    @ApiOperation("查询全部订单")
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        Page<Order> userPage = orderService.findPage(pageNum,pageSize,search);
        return Result.success(userPage);
    }

    @ApiOperation("查询用户订单")
    @GetMapping("/user_order")
    public Result<?> findUserOrder(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "") String search,
                                   @RequestParam(defaultValue = "0") Integer state) {
        Page<Order> userPage = orderService.findUserOrder(pageNum,pageSize,search,state);
        return Result.success(userPage);
    }

    @ApiOperation("根据传入的订单编号查出该订单购买的商品")
    @GetMapping("/selectOrder")
    public Result<?> findOrderByNo(@RequestParam String orderNo){
        List<Order> orders = orderService.findOrderByNo(orderNo);
        return Result.success(orders);
    }

    @ApiOperation("确定收货")
    @PutMapping("/receipt/{id}")
    public Result<?> receipt(@PathVariable("id") Integer id){
        orderService.receipt(id);
        return Result.success();
    }

    @ApiOperation("绘制首页图表")
    @GetMapping("/charts")
    public Result<?> charts() {
        Dict data = orderService.charts();
        return Result.success(data);
    }

    @ApiOperation("下载excel图表")
    @GetMapping("/download")
    public void download(HttpServletResponse response) {
        orderService.download(response);
    }
}
